﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Admin.assets.ueditor.net
{
    /// <summary>
    /// controller 的摘要说明
    /// </summary>
    public class controller : Evan.WebUI.ManagerAshxHelper, IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public override void ProcessRequest(HttpContext context)
        {
            bool IsAdminLogin = Evan.Member.Administrator.IsAdminLogin();
            if (!IsAdminLogin)
            {
                context.Response.Write("{\"state\":\"\",\"url\":\"\",\"title\":\"\",\"original\":\"\",\"error\":\"管理员未登录或登录超时\"}");
                //context.Response.Write("{\"result\" :\"" + "-1" + "\",\"returnval\" :\"" + "管理员未登录" + "\",\"error\":\"管理员未登录或登录超时\"}");
                context.Response.End();
            }
            else
            {

                Handler action = null;
                switch (context.Request["action"])
                {
                    case "config":
                        action = new ConfigHandler(context);
                        break;
                    case "uploadimage":
                        action = new UploadHandler(context, new UploadConfig()
                        {
                            AllowExtensions = Config.GetStringList("imageAllowFiles"),
                            PathFormat = Config.GetString("imagePathFormat"),
                            SizeLimit = Config.GetInt("imageMaxSize"),
                            UploadFieldName = Config.GetString("imageFieldName"),
                            UrlPrefix = Config.GetString("imageUrlPrefix")
                        });
                        break;
                    case "uploadscrawl":
                        action = new UploadHandler(context, new UploadConfig()
                        {
                            AllowExtensions = new string[] { ".png" },
                            PathFormat = Config.GetString("scrawlPathFormat"),
                            SizeLimit = Config.GetInt("scrawlMaxSize"),
                            UploadFieldName = Config.GetString("scrawlFieldName"),
                            Base64 = true,
                            Base64Filename = "scrawl.png",
                            UrlPrefix = Config.GetString("scrawlUrlPrefix")
                        });
                        break;
                    case "uploadvideo":
                        action = new UploadHandler(context, new UploadConfig()
                        {
                            AllowExtensions = Config.GetStringList("videoAllowFiles"),
                            PathFormat = Config.GetString("videoPathFormat"),
                            SizeLimit = Config.GetInt("videoMaxSize"),
                            UploadFieldName = Config.GetString("videoFieldName"),
                            UrlPrefix = Config.GetString("videoUrlPrefix")
                        });
                        break;
                    case "uploadfile":
                        action = new UploadHandler(context, new UploadConfig()
                        {
                            AllowExtensions = Config.GetStringList("fileAllowFiles"),
                            PathFormat = Config.GetString("filePathFormat"),
                            SizeLimit = Config.GetInt("fileMaxSize"),
                            UploadFieldName = Config.GetString("fileFieldName"),
                            UrlPrefix = Config.GetString("fileUrlPrefix")
                        });
                        break;
                    case "listimage":
                        action = new ListFileManager(context, Config.GetString("imageManagerUrlPrefix"), Config.GetString("imageManagerListPath"), Config.GetStringList("imageManagerAllowFiles"));
                        break;
                    case "listfile":
                        action = new ListFileManager(context, Config.GetString("fileManagerUrlPrefix"), Config.GetString("fileManagerListPath"), Config.GetStringList("fileManagerAllowFiles"));
                        break;
                   
                    default:
                        action = new NotSupportedHandler(context);
                        break;
                }
                action.Process();
            }
        }

    }
}